import api from "../../utils/apiInstance"
import { getUserInfo } from '../../utils/store';

const app = getApp();
const moment = app.moment
Page({
  data: {
    baseUrl: app.globalData.baseUrl,
    fileUrl: app.globalData.fileUrl,
    params: {},
    files: [],
    fileList: [],
    fileNameList: [],
    isAdd: false,
    bigSearchShow: false,
    caoZuoList: [],
    shenHeList:['审核通过', '审核意见'],
    shenHeName:"",
    name:getUserInfo().siocCnName,
    form: {
      pass:'',
      expiredDate:moment().format('YYYY-MM-DD'),
      receiveDate:moment().format('YYYY-MM-DD'),
      recheckTime:moment().format('YYYY-MM-DD'),
      depart:getUserInfo().siocDepartCode,
      post:getUserInfo().post,
      staffName:getUserInfo().siocCnName
    }
  },

  //params type ==3 紧急回复
  //type ==4 审核
  async onLoad(params) {
    console.log('params', params);

    this.setData({
      params: params,
      isAdd: !params.id
    })

    if (this.data.isAdd) {
      this.init()
    }

  },
  async loadData() {

  },

  init() {
    this.getCertificateType()
  },

  doBigSearchShow() {
    this.setData({
      bigSearchShow: true
    })
  },



  async getCertificateType() {
    const certificateTypeList = await api.get("/v1/openApi/dic/data/type/ehs_certificate_type")
    const tempArray = [], tempArray1 = []
    certificateTypeList.rows.forEach(item => {
      tempArray.push(...JSON.parse(item.dictValue))
    });

    tempArray.forEach(item => {
      tempArray1.push({ name: item })
    });

    this.setData({
      caoZuoList: tempArray1
    })
  },


  searchSelect(e) {
    this.setData({
      ['form.jobItem']: e.detail.name
    })
  },

  passChange(e){
    console.log("e",e);
    this.setData({
      ['form.pass']: e.detail.value,
      shenHeName:e.detail.value,
    })
  },

  imageUpload(e) {
    this.setData({
      files: e.detail
    })
  },

  contentChange(e) {
    this.setData({
      ['form.content']: e.detail.value
    })
  },

  nameChange(e) {
    this.setData({
      ['form.name']: e.detail.value
    })
  },

  shenFenZChange(e) {
    this.setData({
      ['form.idCard']: e.detail.value
    })
  },
  numChange(e) {
    this.setData({
      ['form.num']: e.detail.value
    })
  },
  issueUnitChange(e) {
    this.setData({
      ['form.issueUnit']: e.detail.value
    })
  },

  receiveDateChange: function (e) {
    this.setData({
      ['form.receiveDate']: e.detail.value
    })
  },
  
  expiredDateChange: function (e) {
    this.setData({
      ['form.expiredDate']: e.detail.value
    })
  },
  recheckTimeChange: function (e) {
    this.setData({
      ['form.recheckTime']: e.detail.value
    })
  },

  

  async doSubmit() {

    let params = {};
    let res = "";

     //审核回复
     if (this.data.params.type == 4) {
      if (!this.data.form.content) {
        wx.showToast({
          title: '请填写内容',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      if (this.data.form.pass=='') {
        console.log('this.data.form.pass',this.data.form.pass);
        wx.showToast({
          title: '请选择是否通过',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      params = {
        certificateId: this.data.params.id,
        content: this.data.form.content,
        pass:this.data.form.pass==0?true:false
      }
      res = await api.post('/v1/openApi/certificate/certificate/answer', params)
      
    }

    //紧急回复
    else if (this.data.params.type == 3) {
      if (!this.data.form.content) {
        wx.showToast({
          title: '请填写内容',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      params = {
        certificateId: this.data.params.id,
        content: this.data.form.content,
        pass:false
      }
      res = await api.post('/v1/openApi/certificate/certificate/answer', params)
      
    }
    else {
      //const zhengJianGqSj = this.selectComponent("#zhengJianGqSj").data
      //修改
      if (!this.data.isAdd) {
        // if (this.data.files.length == 0) {
        //   wx.showToast({
        //     title: '请上传图片',
        //     icon: 'none',
        //     duration: 2000
        //   })
        //   return;
        // }
        if (this.data.fileList.length == 0) {
          wx.showToast({
            title: '请上传文件',
            icon: 'none',
            duration: 2000
          })
          return;
        }

        params = {
          // files: this.data.files,
          files: this.data.fileList,
          id: this.data.params.id,
          ...this.data.form
        }
        res = await api.post('/v1/openApi/certificate/change', params)
      }
      //添加
      else {
        // const zhengJianSj = this.selectComponent("#zhengJianSj").data
        // if (!this.data.form.idCard) {
        //   wx.showToast({
        //     title: '请填写身份证',
        //     icon: 'none',
        //     duration: 2000
        //   })
        //   return;
        // }
        if (!this.data.form.jobItem) {
          wx.showToast({
            title: '请选择操作项目',
            icon: 'none',
            duration: 2000
          })
          return;
        }
        if (!this.data.form.num) {
          wx.showToast({
            title: '请填写证书编号',
            icon: 'none',
            duration: 2000
          })
          return;
        }
        if (!this.data.form.issueUnit) {
          wx.showToast({
            title: '请填写签发机关',
            icon: 'none',
            duration: 2000
          })
          return;
        }
        if (this.data.fileList.length == 0) {
          wx.showToast({
            title: '请上传文件',
            icon: 'none',
            duration: 2000
          })
          return;
        }
        params = {
          files: this.data.fileList,
          name:this.data.form.jobItem,
          ...this.data.form
        }
        res = await api.post('/v1/openApi/certificate/upload/certificate', params)
      }
    }
    wx.showToast({
      title: '提交成功',
      icon: 'none',
      duration: 1000
    })
    setTimeout(() => {
      wx.navigateTo({
        url: '/subPages/zhengJianGl/zhengJianGl'
      });
    }, 1000)
  },

  chooseFile () {
    var that = this;
    wx.chooseMessageFile({
      count: 1,
      type: 'all',
      async success(res) { // 将图片上传到服务器
        let tempFile = res.tempFiles[0];
        wx.uploadFile({
          url: that.data.fileUrl + '/common/picUpload?name='+tempFile.name, // 上传的服务器地址
          filePath: tempFile.path, // 图片的本地地址
          name: 'file', // 服务端用于接收文件的参数名
          success(res) {
            let json = JSON.parse(res.data);
            let tempArray = [];
            tempArray.push(json.fileName);
            let tempArray2 = [];
            tempArray2.push(tempFile.name);
            // 上传成功，做一些处理
            if (that.data.fileList.length != 0) {
              that.setData({
                fileList: that.data.fileList.concat(tempArray),
                fileNameList: that.data.fileNameList.concat(tempArray2),
              })
            } else {
              that.setData({
                fileList: tempArray,
                fileNameList: tempArray2
              })
            }
          }
        })
      }
    })
  },
  delFile(e) {
    wx.showModal({
      title: '删除文件',
      content: '确定要删除这个文件',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.fileList.splice(e.currentTarget.dataset.index, 1);
          this.data.fileNameList.splice(e.currentTarget.dataset.index, 1);
          
          this.setData({
            fileList: this.data.fileList,
            fileNameList:this.data.fileNameList
          })
        }
      }
    })
  }
})
